package com.fgba.express.controller;


import com.fgba.express.comm.exception.BizException;
import com.fgba.express.comm.result.ResultCode;
import com.fgba.express.core.controller.BaseController;
import com.fgba.express.data.dto.RoleDto;
import com.fgba.express.data.entity.Role;
import com.fgba.express.data.mapstruct.RoleStruct;
import com.fgba.express.data.search.RoleSearch;
import com.fgba.express.data.service.IMenuService;
import com.fgba.express.data.service.IRoleService;
import com.fgba.express.data.service.IUserService;
import com.fgba.express.util.BeanContextUtil;
import com.fgba.express.util.NullChecker;
import com.fgba.express.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 角色 前端控制器
 * </p>
 *
 * @author fgba
 * @since 2022-02-24
 */
@RestController
@RequestMapping("/security/role")
@Api(value = "角色相关操作", tags = "角色相关操作")
public class RoleController extends BaseController<Role, RoleDto, RoleSearch, IRoleService, RoleStruct> {

    @Override
    public void createValidation(RoleDto model) {
        if (NullChecker.isNullOrEmpty(model.getName())) {
            log.error("RoleController -> save: name is null");
            throw new BizException(ResultCode.USER_REQUEST_PARAM_IS_BLANK);
        }

        if (NullChecker.isNullOrEmpty(model.getCode())) {
            log.error("RoleController -> save: code is null");
            throw new BizException(ResultCode.USER_REQUEST_PARAM_IS_BLANK);
        }

    }

    /**
     * 分配角色菜单
     *
     * @return
     */
    @ApiOperation(value = "分配角色菜单", notes = "分配角色菜单")
    @PostMapping("/assign/{roleId}")
    public Boolean assignMenu(@PathVariable Long roleId, @RequestBody List<Long> menuIds) {

        boolean success = service.assignMenu(roleId, menuIds);
        if (success) {
            BeanContextUtil.getBean(IUserService.class).clearUserAuthorityInfoByRoleId(roleId);
        }
        return success;
    }
}
